package com.yksj.healthtalk.ui.server;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonHttpResponseHandler;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.StringFormatUtils;

/**
 * 中心介绍
 * @author zhao
 */
public class ServerCenterIntroduceFragment extends Fragment implements OnClickListener {
//	ImageLoader mImageLoader;
	JSONObject mDataJsonObject;
	ImageView mLogoImageV;
	TextView mNameTxtV;
	TextView mDetailTxtV;
	TextView mPhoneTxtV;
	TextView mAddressTxtV;
	TextView mFixTextV;
	TextView mZipTextV;
	double lat;
	double lon;
	
	final JsonHttpResponseHandler mResponseHandler = new JsonHttpResponseHandler(){
		public void onSuccess(int statusCode, org.json.JSONObject response) {
			mDataJsonObject = response;
			onParseData();
		};
	};
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.server_center_introduce_layout,null);
		mLogoImageV = (ImageView)view.findViewById(R.id.logo_imge);
		mNameTxtV = (TextView)view.findViewById(R.id.name_txtv);
		mDetailTxtV = (TextView)view.findViewById(R.id.detail_txtv);
		mPhoneTxtV = (TextView)view.findViewById(R.id.phone_txtv);
		mAddressTxtV = (TextView)view.findViewById(R.id.address_txtv);
		mFixTextV = (TextView)view.findViewById(R.id.fix_txtv);
		mZipTextV = (TextView)view.findViewById(R.id.zipcode_txtv);
		BMapApiServerFragment fragment = new BMapApiServerFragment();
		getFragmentManager().beginTransaction().replace(R.id.map,fragment).commit();
		view.findViewById(R.id.map_imge).setOnClickListener(this);
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		HttpRestClient.doHttpQueryServerCenterIntroduce(getArguments().getString("id"),mResponseHandler);
	
	}
	/**
	 * 解析数据
	 * @param data
	 */
	private void onParseData(){
		try {
			String address = StringFormatUtils.ObjectToString(mDataJsonObject.getString("ADDRESS"));
			String phone = StringFormatUtils.ObjectToString(mDataJsonObject.getString("TELEPHONE"));
			String zip =StringFormatUtils.ObjectToString(mDataJsonObject.getString("ZIP_CODE")); 
			String fax =StringFormatUtils.ObjectToString(mDataJsonObject.getString("FAX_CODE")); 
			String name = StringFormatUtils.ObjectToString(mDataJsonObject.getString("MERCHANT_NAME")); 
			String detail=StringFormatUtils.ObjectToString(mDataJsonObject.getString("MERCHANT_DESC"));
			
			mZipTextV.setText(zip);
			mFixTextV.setText(fax);
			mPhoneTxtV.setText(phone);
			mDetailTxtV.setText(detail);
			mNameTxtV.setText(name);
			mAddressTxtV.setText(address);
//			mImageLoader.displayImage(mDataJsonObject.getString("LOGO_ICON"), mLogoImageV, mImageOptions);
			
			if(mDataJsonObject.has("listmap")){
				JSONArray array = mDataJsonObject.getJSONArray("listmap");
				if(array.length() == 0)return;
				JSONObject jsonObject = array.getJSONObject(0);
				BMapApiServerFragment serverFragment = (BMapApiServerFragment)
				getFragmentManager().findFragmentById(R.id.map);
				lat=jsonObject.optDouble("LAT",0.0);
				lon=jsonObject.optDouble("LNG",0.0);
				serverFragment.initOverlay(lat, lon,getArguments().getString("title"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.map_imge:
			Intent intent=new Intent(getActivity(),BMapApiServerFragmentActivity.class);
			Bundle bundle = new Bundle();
			bundle.putDouble("lat",lat);
			bundle.putDouble("lon",lon);
			bundle.putString("title", getArguments().getString("title"));
			intent.putExtras(bundle);
			startActivity(intent);
			break;
		}
		
	}
}
